$(function () {
    //地址
    $(".address_big_bg dl").eq(2).tap(function(){
        var display_city = $(".city_show").css("display");
        if(display_city == "none"){
            $(".opa_background").show();
            $(".city_show").show();
            $(".city_show_news ul li").remove();
            //获取省
            $.ajax({
                type:"get",
                url:httpHost+"/Public/getAreaChildren?parent_id=1",
                success: function(data){
                    province(data);
                    $(".city_show_news li").tap(function(){
                        var province_id = $(this).attr("id");
                        $('input[name=province]').val(province_id);
                        var province_text = $(this).text();
                        //获取市
                        $.ajax({
                            type:"get",
                            url:httpHost+"/Public/getAreaChildren?parent_id="+ province_id +"",
                            success: function(data){
                                city(data);
                                $(".city_show_news li").tap(function(){
                                    var city_id = $(this).attr("id");
                                    $('input[name=city]').val(city_id);
                                    var city_text = $(this).text();
                                    //获取区
                                    $.ajax({
                                        type:"get",
                                        url:httpHost+"/Public/getAreaChildren?parent_id="+ city_id +"",
                                        success: function(data){
                                            district(data);

                                            if(data == ""){
                                                $(".opa_background").hide();
                                                $(".city_show").hide();
                                                $('input[name=district]').val("");
                                                $(".tap_city").attr("value",province_text + " " + city_text);
                                            }
                                            else{
                                                $(".city_show_news li").tap(function(){
                                                    var district_id = $(this).attr('id');
                                                    $('input[name=district]').val(district_id);
                                                    var district_text = $(this).text();
                                                    $(".opa_background").hide();
                                                    $(".city_show").hide();
                                                    $(".tap_city").attr("value",province_text + " " + city_text + " " + district_text);
                                                });
                                            }
                                        },
                                        error:function(){
                                            Ddivok1("服务器连接超时，请刷新！");
                                        }
                                    });
                                });
                            },
                            error:function(){
                                Ddivok1("服务器连接超时，请刷新！");
                            }
                        });
                    });
                },
                error:function(){
                    Ddivok1("服务器连接超时，请刷新！");
                }
            });
        }
    });

    //保存
    var address = 0;
    $(".address_news_button").tap(function(){
        if(address == 0){
            address = 1;
            var address_name = $(".address_name").val();
            var address_phone = $(".address_phone").val();
            var tap_city = $("input[name=city]").val();
            var address_area = $(".address_area").val();
            if(address_name == "" || address_name.length <= 0){
                Ddivok1("请正确填写收货人姓名");
            }else if(address_phone == "" || address_phone.length <= 0){
                Ddivok1("请正确填写联系电话");
            }else if(tap_city == "" || tap_city.length <= 0){
                Ddivok1("请选择地区");
            }else if(address_area == "" || address_area.length <= 0){
                Ddivok1("请正确填写街道、楼牌号");
            }else{
                localStorage.setItem("tihuo_show",1);
                formAjaxSubmit();
            }
            setTimeout(function(){address = 0;},5000);
        }
    });

    //关闭选择地址
    $(".opa_background").tap(function(){
        $(".opa_background").hide();
        $(".city_show").hide();
    });
});

//省
function province(data){
    $(".city_show_news strong").text("所在省/市/自治区");
    for(var i=0; i<data.length; i++){
        var jsondata = data[i];
        var Dhtml = '<li id='+ jsondata.id +'>'+ jsondata.area +'</li>';
        $(".city_show_news ul").append(Dhtml);
    }
}

//市
function city(data){
    $(".city_show_news strong").text("所在市/盟");
    $(".city_show_news ul li").remove();
    for(var i=0; i<data.length; i++){
        var jsondata = data[i];
        var Dhtml = '<li id='+ jsondata.id +'>'+ jsondata.area +'</li>';
        $(".city_show_news ul").append(Dhtml);
    }
}

//区
function district(data){
    $(".city_show_news strong").text("所在县/区");
    $(".city_show_news ul li").remove();
    for(var i=0; i<data.length; i++){
        var jsondata = data[i];
        var Dhtml = '<li id='+ jsondata.id +'>'+ jsondata.area +'</li>';
        $(".city_show_news ul").append(Dhtml);
    }
}